package cn.yunyichina.provider.pay.service;

import cn.yunyichina.utils.commons.RandomUtil;
import cn.yunyichina.utils.security.SignUtil;

import java.util.HashMap;

/**
 * Created by Lullaby on 2016/10/20.
 */
public class BaseNotifyService extends BaseService {

    /**
     * 微信支付通知返回微信成功报文
     *
     * @return
     */
    protected String printWechatPayNotifySuccessMessage() {
        StringBuffer success = new StringBuffer();
        success.append("<xml>").
                append("<return_code><![CDATA[SUCCESS]]></return_code>").
                append("<return_msg><![CDATA[OK]]></return_msg>").
                append("</xml>");
        return success.toString();
    }

    /**
     * 微信支付通知返回微信失败报文
     *
     * @return
     */
    protected String printWechatPayNotifyFailureMessage() {
        StringBuffer failure = new StringBuffer();
        failure.append("<xml>").
                append("<return_code><![CDATA[FAIL]]></return_code>").
                append("<return_msg><![CDATA[NOTOK]]></return_msg>").
                append("</xml>");
        return failure.toString();
    }

    /**
     * 微信医疗支付通知返回微信成功报文
     *
     * @return
     */
    protected String printWechatMedicalPayNotifyMessage(boolean isSuccess, String key) {
        StringBuffer success = new StringBuffer();

        String returnCode = "SUCCESS";
        String returnMsg = "OK";
        String resultCode = isSuccess ? "SUCCESS" : "FAIL";
        String nonceStr = RandomUtil.getRandomALLChar(32);

        HashMap<String, String> params = new HashMap<>();
        params.put("return_code", returnCode);
        params.put("return_msg", returnMsg);
        params.put("result_code", resultCode);
        params.put("nonce_str", nonceStr);

        String sign = SignUtil.wechatPaySign(params, key);

        success.append("<xml>").
                append("<return_code><![CDATA[").append(returnCode).append("]]></return_code>").
                append("<return_msg><![CDATA[").append(returnMsg).append("]]></return_msg>").
                append("<result_code><![CDATA[").append(resultCode).append("]]></result_code>").
                append("<nonce_str><![CDATA[").append(nonceStr).append("]]></nonce_str>").
                append("<sign><![CDATA[").append(sign).append("]]></sign>").
                append("</xml>");

        return success.toString();
    }

    /**
     * 平安付响应通知成功
     *
     * @return
     */
    protected String printPacityPayNotifySuccessMessage() {
        return "{\"returnCode\":\"SUCCESS\"}";
    }

    /**
     * 平安付响应通知失败
     *
     * @param returnMsg
     * @return
     */
    protected String printPacityPayNotifyFailMessage(String returnMsg) {
        return "{\"returnCode\":\"FAIL\",\"returnMsg\":\"" + returnMsg + "\"}";
    }

}
